package com.china08.yxyapi.schedule.attendance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.entity.mg.attendance.YxyStuAttendance;
import com.china08.yxyapi.repository.mg.attendance.YxyStuAttendanceRepository;
import com.china08.yxyapi.util.DateUtils;
import com.china08.yxyapi.util.RabbitMqSendUtil;
import com.china08.yxyapi.util.YxyScheduleRunnableUtil;

@Component
public class StuAttendanceStatistics {
    private static final Logger LOGGER = Logger.getLogger(StuAttendanceStatistics.class);
    @Autowired
    YxyStuAttendanceRepository yxyStuAttendanceRepository;

    /**
     * 学生到班考勤, 每天晚上 00:15:00 统计前一天数据
     */
    @Scheduled(cron = "0 15 0 * * ?")
    public void attendanceStatistics() {
        // 1: 查询哪些学校需要进行统计数据, 查询考勤配置表 首先考勤配置表中需要有配置信息才进行统计.
        // 2: 将学校id 放入到 RabbitMQ中

        // ##
        // 检测定时任务是否可执行
        if (!YxyScheduleRunnableUtil.checkRunnable()) { return; }

        Integer beforDate = DateUtils.formatDate4(DateUtils.getBeforeDay(new Date()));

        List<YxyStuAttendance> signList = yxyStuAttendanceRepository.findByDayDate(beforDate);
        List<String> sList = new ArrayList<String>();
        for (YxyStuAttendance yxyStuAttendance : signList) {
            if (!sList.contains(yxyStuAttendance.getSchoolId())) {
                sList.add(yxyStuAttendance.getSchoolId());
            }
        }
        if (sList != null && sList.size() > 0) {
            RabbitMqSendUtil.attendanceStatisticsForStudent(sList, beforDate);
        }
    }

    /**
     * 考勤统计, 每天晚上 00:15:00 统计前一天数据
     */
    /*
     * @Scheduled(cron = "0/20 * * * * ?") public void
     * attendanceStatisticsTest() { // 1: 查询哪些学校需要进行统计数据, 查询考勤配置表
     * 首先考勤配置表中需要有配置信息才进行统计. // 2: 将学校id 放入到 RabbitMQ中 // try { // String
     * hostname = (InetAddress.getLocalHost()).getHostName(); // if
     * (!"YxYAPI01".equals(hostname) && !"yxapiyx01".equals(hostname) && //
     * !"YxConvirt01".equals(hostname)) { // // LOGGER.info("HostName:" +
     * hostname); // return; // } // } catch (UnknownHostException e) { // // }
     * 
     * Integer beforDate = DateUtils.formatDate4(DateUtils.getBeforeDay(new
     * Date())); List<String> lTecAttendanceConfig =
     * yxyTecAttendanceConfigRepository.findSchoolIdAll();
     * 
     * for (int i = 1; i <= 7; i++) { if (lTecAttendanceConfig != null &&
     * lTecAttendanceConfig.size() > 0) {
     * RabbitMqSendUtil.attendanceStatistics(lTecAttendanceConfig,
     * DateUtils.formatDate4(org.apache.commons.lang3.time.DateUtils.addDays(new
     * Date(), i))); } }
     * 
     * for (int i = 0; i >= -7; i--) { if (lTecAttendanceConfig != null &&
     * lTecAttendanceConfig.size() > 0) {
     * RabbitMqSendUtil.attendanceStatistics(lTecAttendanceConfig,
     * DateUtils.formatDate4(org.apache.commons.lang3.time.DateUtils.addDays(new
     * Date(), i))); } } }
     */

    // public static void main(String[] args) {
    // Integer day =
    // DateUtils.formatDate4(org.apache.commons.lang3.time.DateUtils.addDays(new
    // Date(), 1));
    // System.out.println(day);
    //
    // }
}
